package com.hrhx.duhongming.adapter;

import java.util.Map;

import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.LineType;
import com.github.abel533.echarts.code.PointerType;
import com.github.abel533.echarts.code.Position;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Symbol;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.style.LineStyle;
import com.hrhx.duhongming.bean.EchartsConfig;

public class EchartsDoubleNumLineAdapter implements EchartsAdapter{
	
	//line and bar的适配器入口
	public static GsonOption adapter(EchartsConfig echartsConfig,
			GsonOption gsonOption,
			Map<String,Object[][]> data){
		EchartsDoubleNumLineAdapter barAdapter = new EchartsDoubleNumLineAdapter();
		gsonOption = barAdapter.tooltipAdapter(gsonOption);
		gsonOption = barAdapter.legendAdapter(gsonOption, data);
		gsonOption = barAdapter.xAxisAdapter(echartsConfig,gsonOption);
		gsonOption = barAdapter.yAxisAdapter(echartsConfig,gsonOption);
		gsonOption = barAdapter.seriesAdapter(echartsConfig,gsonOption, data);
		return gsonOption;
	}
	
	public GsonOption tooltipAdapter(GsonOption gsonOption){
		gsonOption.calculable(true);
		gsonOption.tooltip().axisPointer().show(true).type(PointerType.cross)
		.lineStyle(new LineStyle().type(LineType.dashed).width(1));
		return gsonOption;
	}
	//legend
	public GsonOption legendAdapter(GsonOption gsonOption,Map<String,Object[][]> data){
		if(data != null && data.size() != 0){
			//获取第一个List中的Map中的值
			for (String key : data.keySet()) {
				gsonOption.legend().data(key);
			}
		}
		return gsonOption;
	}
	
	//x轴配置以及单位配置
	public GsonOption xAxisAdapter(EchartsConfig echartsConfig,GsonOption gsonOption){
		gsonOption.xAxis(new ValueAxis().type(AxisType.value).name(echartsConfig.getXaxisName()));
		return gsonOption;
	}
	
	//Y轴单位配置
	public GsonOption yAxisAdapter(EchartsConfig echartsConfig,GsonOption gsonOption){
		gsonOption.yAxis(new CategoryAxis().type(AxisType.value).name(echartsConfig.getYaxisName()));
		return gsonOption;
	}
	
	//Y轴数据处理
	@SuppressWarnings("unchecked")
	public GsonOption seriesAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,Map<String,Object[][]> axisDataArr){
		for (String mapkey : axisDataArr.keySet()) {
			Line line = new Line();
			// 显示直线，而不是密密麻麻的点，一点都不好看
			line.name(mapkey).type(SeriesType.line).symbol(Symbol.none);
			line.itemStyle().normal().label().show(true).position(Position.top);
			Object[][] dataArr = axisDataArr.get(mapkey);
			for (int num = 0; num < dataArr.length; num++) {
				line.data().add(dataArr[num]);
			}
//			if (mapkey.startsWith("y0")) {
//				line.yAxisIndex(0);
//			} else {
//				line.yAxisIndex(1);
//			}
			gsonOption.series(line);
		}
		return gsonOption;
	}
}
